#include <iostream>
#include <string>
#include <fstream>
#include <vector>

// 简单的CBP文件解析器（不使用TinyXML，避免依赖问题）
class SimpleCBParser {
public:
    static bool ParseCBPFile(const std::string& filename, std::string& projectName, std::vector<std::string>& sourceFiles) {
        std::ifstream file(filename);
        if (!file.is_open()) {
            return false;
        }
        
        std::string line;
        bool inProject = false;
        
        while (std::getline(file, line)) {
            // 查找项目标题
            if (line.find("<Option title=") != std::string::npos) {
                size_t start = line.find('"');
                size_t end = line.find('"', start + 1);
                if (start != std::string::npos && end != std::string::npos) {
                    projectName = line.substr(start + 1, end - start - 1);
                }
            }
            
            // 查找源文件
            if (line.find("<Unit filename=") != std::string::npos) {
                size_t start = line.find('"');
                size_t end = line.find('"', start + 1);
                if (start != std::string::npos && end != std::string::npos) {
                    std::string filePath = line.substr(start + 1, end - start - 1);
                    if (filePath.find(".cpp") != std::string::npos || 
                        filePath.find(".c") != std::string::npos ||
                        filePath.find(".h") != std::string::npos) {
                        sourceFiles.push_back(filePath);
                    }
                }
            }
        }
        
        file.close();
        return !projectName.empty();
    }
};

// AI模型管理器
class AIModelManager {
private:
    std::string currentModel;
    std::string currentModelName;
    
public:
    AIModelManager() : currentModel("DSAI"), currentModelName("DS-V3.2exp0922") {}
    
    void SetModel(int model_id, const std::string& model_name = "") {
        switch(model_id) {
            case 0: 
                currentModel = "DSAI";
                currentModelName = model_name.empty() ? "DS-V3.2exp0922" : model_name;
                break;
            case 1:
                currentModel = "UEAI";
                currentModelName = model_name.empty() ? "UE-CodeExpert" : model_name;
                break;
            case 2:
                currentModel = "OPAI";
                currentModelName = model_name.empty() ? "GPT-4" : model_name;
                break;
            default:
                currentModel = "UNKNOWN";
                currentModelName = "Unknown";
        }
        std::cout << "✅ AI模型设置: " << currentModel << " - " << currentModelName << std::endl;
    }
    
    std::string GetStatus() const {
        return "AI模型: " + currentModel + " (" + currentModelName + ")";
    }
    
    std::string AnalyzeProject(const std::string& cbpFile) {
        std::string projectName;
        std::vector<std::string> sourceFiles;
        
        if (SimpleCBParser::ParseCBPFile(cbpFile, projectName, sourceFiles)) {
            std::string analysis = "🔍 AI分析报告 (基于CBP文件):\n";
            analysis += "   项目名称: " + projectName + "\n";
            analysis += "   文件数量: " + std::to_string(sourceFiles.size()) + "\n";
            analysis += "   使用的模型: " + currentModelName + "\n";
            
            if (!sourceFiles.empty()) {
                analysis += "   文件列表:\n";
                for (size_t i = 0; i < sourceFiles.size() && i < 8; ++i) {
                    analysis += "     - " + sourceFiles[i] + "\n";
                }
                if (sourceFiles.size() > 8) {
                    analysis += "     ... 还有 " + std::to_string(sourceFiles.size() - 8) + " 个文件\n";
                }
            }
            
            // 添加AI建议
            analysis += "\n💡 AI建议:\n";
            analysis += "   - 检查编译依赖关系\n";
            analysis += "   - 优化Makefile结构\n";
            analysis += "   - 验证跨平台兼容性\n";
            
            return analysis;
        } else {
            return "❌ 无法解析CBP文件或文件不存在: " + cbpFile;
        }
    }
};

int main(int argc, char* argv[]) {
    AIModelManager aiManager;
    
    // AI模型设置命令
    if (argc > 1 && std::string(argv[1]) == "--ai-set-model") {
        if (argc > 3) {
            int model_id = std::stoi(argv[2]);
            std::string model_name = argv[3];
            aiManager.SetModel(model_id, model_name);
        } else if (argc > 2) {
            int model_id = std::stoi(argv[2]);
            aiManager.SetModel(model_id);
        } else {
            std::cout << "用法: --ai-set-model <模型ID> [模型名称]" << std::endl;
            std::cout << "模型ID:" << std::endl;
            std::cout << "  0 - DeepSeek AI (默认: DS-V3.2exp0922)" << std::endl;
            std::cout << "  1 - UE CodeExpert" << std::endl;
            std::cout << "  2 - GPT-4" << std::endl;
            std::cout << "示例:" << std::endl;
            std::cout << "  --ai-set-model 0 DS-V3.2exp0922" << std::endl;
            std::cout << "  --ai-set-model 1 UE-CodeExpert" << std::endl;
            std::cout << "  --ai-set-model 2 GPT-4" << std::endl;
        }
        return 0;
    }
    
    // AI模式 - CBP文件分析
    if (argc > 1 && std::string(argv[1]) == "--ai-mode") {
        if (argc > 2) {
            std::string cbpFile = argv[2];
            std::cout << aiManager.AnalyzeProject(cbpFile) << std::endl;
        } else {
            std::cout << "用法: --ai-mode <CBP文件路径>" << std::endl;
            std::cout << "示例: --ai-mode project.cbp" << std::endl;
        }
        return 0;
    }
    
    // AI状态显示
    if (argc > 1 && std::string(argv[1]) == "--ai-status") {
        std::cout << aiManager.GetStatus() << std::endl;
        return 0;
    }
    
    // 显示模型列表
    if (argc > 1 && std::string(argv[1]) == "--ai-list-models") {
        std::cout << "可用AI模型:" << std::endl;
        std::cout << "  0: DeepSeek AI (DS-V3.2exp0922)" << std::endl;
        std::cout << "  1: UE CodeExpert" << std::endl;
        std::cout << "  2: GPT-4" << std::endl;
        return 0;
    }
    
    // 帮助信息
    if (argc > 1 && (std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h")) {
        std::cout << "cbp2make_ai - AI增强的CBP到Makefile转换器" << std::endl;
        std::cout << "版本: 1.0 (AI增强版)" << std::endl;
        std::cout << std::endl;
        std::cout << "AI功能命令:" << std::endl;
        std::cout << "  --ai-set-model <id> [name]  设置AI模型和版本" << std::endl;
        std::cout << "  --ai-mode <file.cbp>        AI分析CBP项目文件" << std::endl;
        std::cout << "  --ai-status                显示当前AI模型状态" << std::endl;
        std::cout << "  --ai-list-models           显示可用AI模型列表" << std::endl;
        std::cout << std::endl;
        std::cout << "传统功能命令:" << std::endl;
        std::cout << "  -in <file.cbp>             输入CBP文件" << std::endl;
        std::cout << "  -out <makefile>            输出makefile" << std::endl;
        std::cout << "  --help, -h                 显示此帮助信息" << std::endl;
        std::cout << std::endl;
        std::cout << "使用示例:" << std::endl;
        std::cout << "  cbp2make_ai --ai-set-model 0 DS-V3.2exp0922" << std::endl;
        std::cout << "  cbp2make_ai --ai-mode myproject.cbp" << std::endl;
        std::cout << "  cbp2make_ai --ai-status" << std::endl;
        std::cout << "  cbp2make_ai --ai-list-models" << std::endl;
        return 0;
    }
    
    // 默认行为 - 显示欢迎信息
    std::cout << "🚀 cbp2make_ai - AI增强版本已就绪!" << std::endl;
    std::cout << "💡 使用 --help 查看所有AI功能" << std::endl;
    std::cout << "🤖 当前AI模型: " << aiManager.GetStatus() << std::endl;
    
    return 0;
}
